package com.weitu.occupationalHealth.phyData.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.weitu.accessory.service.AccessoryUtilService;
import com.weitu.occupationalHealth.account.mapper.AccountMapper;
import com.weitu.occupationalHealth.phyData.entity.PhyData;
import com.weitu.occupationalHealth.phyData.mapper.*;
import com.weitu.occupationalHealth.phyData.service.PhyDataService;

import com.weitu.util.IDGenerator;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PhyDataServiceImpl implements PhyDataService {
    @Autowired
    private PhyDataMapper phyDataMapper;

    @Autowired
    private OccForbidMapper occForbidMapper;

    @Autowired
    private OccDoubtMapper occDoubtMapper;

    @Autowired
    private OccOtherMapper occOtherMapper;

    @Autowired
    private ReviewInfoMapper reviewInfoMapper;

    @Autowired
    private DiagnosisMapper diagnosisMapper;

    @Autowired
    private TgStationMapper tgStationMapper;

    @Autowired
    private AccountMapper accountMapper;

    @Autowired
    private AccessoryUtilService accessoryUtilService;

    @Override
    public PhyData selectById(String id) {
        return this.phyDataMapper.selectById(id);
    }

    @Override
    public int deleteById(String id) {
        return 0;
    }

    @Override
    @Transactional
    public int deleteByIdAll(String id) {
        this.occForbidMapper.deleteById(id);
        this.occDoubtMapper.deleteById(id);
        this.occOtherMapper.deleteById(id);
        this.tgStationMapper.deleteById(id);
        this.diagnosisMapper.deleteById(id);
        this.reviewInfoMapper.deleteById(id);
        this.phyDataMapper.deleteHarmIds(id);
        return this.phyDataMapper.deleteById(id);
    }

    @Override
    public int insertConclusionsState(List<Map> maps) {
        return this.phyDataMapper.insertConclusionsState(maps);
    }


    @Override
    @Transactional
    public boolean insertYcData(Map<String, Object> paramMap) {
        String phyId=paramMap.get("phyId").toString();
        boolean phyState=false;
        if (paramMap.get("remarks")!=null){
            if (!"".equals(paramMap.get("remarks").toString().trim())){
                String remarks=paramMap.get("remarks").toString();
                Map<String, Object> paramPhyMap = new HashMap<String, Object>();
                paramPhyMap.put("id",phyId);
                paramPhyMap.put("remarks",remarks);
                int intphyState=phyDataMapper.updateByIdSelective(paramPhyMap);
                phyState=true;
            }else {
                phyState=true;
            }
        }else {
            phyState=true;
        }

        boolean cState=false;
        if (paramMap.get("yConclusion")!=null){
            List<Map> list=new ArrayList<Map>();
            if (paramMap.get("yConclusion")instanceof String){
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("phyId",phyId);
                map.put("conclusionId",paramMap.get("yConclusion").toString());
                map.put("delState",0);
                list.add(map);
            }else {
                List<String> conIds=(List<String>)paramMap.get("yConclusion");
                if(conIds!=null&& !"".equals(conIds)){
                    if (conIds.size()>0){
                        for (String conid:conIds){
                            Map<String, Object> map = new HashMap<String, Object>();
                            map.put("phyId",phyId);
                            map.put("conclusionId",conid);
                            map.put("delState",0);
                            list.add(map);
                        }
                    }
                }
            }
            int dincCon=phyDataMapper.deleteConclusionsStateById(phyId);
            int intCon=phyDataMapper.insertConclusionsState(list);
            if (intCon>0){
                cState=true;
            }
        }
        boolean editRe=addEditTables(paramMap);

        return cState&&editRe&&phyState;
    }

    @Override
    public int deleteConclusionsStateById(String id) {
        return this.phyDataMapper.deleteConclusionsStateById(id);
    }

    @Override
    public int insertHarmIds(List<Map> maps) {
        return this.phyDataMapper.insertHarmIds(maps);
    }

    @Override
    public int deleteHarmIds(String id) {
        return this.phyDataMapper.deleteHarmIds(id);
    }

    @Override
    public List<Map<String, Object>> selectAnalysis(Map<String, Object> map) {
        return this.phyDataMapper.selectAnalysis(map);
    }

    @Override
    @Transactional
    public boolean deleteYcData(String id) {
        int dintRe=reviewInfoMapper.deleteById(id);
        int dintDe=diagnosisMapper.deleteById(id);
        int dtg=tgStationMapper.deleteById(id);
        int dincCon=phyDataMapper.deleteConclusionsStateById(id);
        return true;
    }


    private Boolean addEditTables(Map<String,Object> paramMap){
        String phyId=paramMap.get("phyId").toString();
        String dateTime=paramMap.get("addTime").toString();
        String loginUserId=paramMap.get("addUserId").toString();
        String loginUserDeptId=paramMap.get("addDeptId").toString();
        String userId=paramMap.get("userId").toString();
        boolean occReviewTbFalg=false;
        boolean occZdTbFalg=false;
        boolean occTg=false;
        if (paramMap.get("occReviewTb")!=null){
            String dataStr = paramMap.get("occReviewTb").toString();
            List<Map> list=addEditTableLists(dataStr,phyId,dateTime,loginUserId,loginUserDeptId,true);
            paramMap.put("reList",list);
            int dintRe=reviewInfoMapper.deleteById(phyId);
            int intRe= reviewInfoMapper.insert(paramMap);
            if (intRe>0){
                occReviewTbFalg=true;
            }
        }else {
            int dintRe=reviewInfoMapper.deleteById(phyId);
            occReviewTbFalg=true;
        }
        if (paramMap.get("occZdTb")!=null){
            String dataStr = paramMap.get("occZdTb").toString();
            List<Map> list=addEditTableLists(dataStr,phyId,dateTime,loginUserId,loginUserDeptId,false);
            paramMap.put("zdList",list);
            int dintRe=diagnosisMapper.deleteById(phyId);
            int intRe= diagnosisMapper.insert(paramMap);
            for (Map map:list){
                if (map.get("isZy")!=null&&"isZy".equals(map.get("isZy").toString())){
                    String uuid = IDGenerator.getId();
                    map.put("id",uuid);
                    map.put("userId",userId);
                    int acc= accountMapper.insertSelective(map);
                }
                if (!map.get("id").toString().equals(map.get("oldid").toString())){
                    boolean f= this.accessoryUtilService.updateBusinessIdByBusinessId(map.get("id").toString(),map.get("oldid").toString());
                }
            }
            if (intRe>0){
                occZdTbFalg=true;
            }
        }else {
            int dintRe=diagnosisMapper.deleteById(phyId);
            occZdTbFalg=true;
        }

        String uuid = IDGenerator.getId();
        paramMap.put("id",uuid);
        int dtg=tgStationMapper.deleteById(phyId);
        int tg=tgStationMapper.insertSelective(paramMap);
        if(tg>0){
            occTg=true;
        }
        return occReviewTbFalg&&occZdTbFalg&&occTg;
    }
    private List<Map> addEditTableLists(String dataStr,String phyId,String dateTime,String loginUserId,String loginUserDeptId,boolean isId){
        List<Map> list = JSONArray.parseArray(dataStr,Map.class);
        for(Map m:list){
            if (isId){
                String uuid = IDGenerator.getId();
                m.put("id",uuid);
            }

            m.put("delState",0);
            m.put("phyDataId",phyId);
            m.put("addTime", dateTime);
            m.put("addUserId", loginUserId);
            m.put("addDeptId", loginUserDeptId);
        }
        return list;
    }

    @Override
    public int updateByIdSelective(Map record) {
        return this.phyDataMapper.updateByIdSelective(record);
    }

    @Override
    public int insertSelective(Map record) {

        return this.phyDataMapper.insertSelective(record);
    }

    @Override
    public List<PhyData> selectAllPhy(Map map) {
        return this.phyDataMapper.selectAllPhy(map);
    }

    @Override
    public int selectAllPhyCount(Map map) {
        return this.phyDataMapper.selectAllPhyCount(map);
    }

    @Override
    public int insertConclusions(List<Map> maps) {

        return this.phyDataMapper.insertConclusions(maps);
    }

    @Override
    public int deleteConclusions(String id) {
        return this.phyDataMapper.deleteConclusions(id);
    }
}